<?php
/** 登录逻辑处理
 * Created by PhpStorm.
 * User: Administrator--Reer
 * Date: 2019/9/11 0011
 * Time: 10:00
 */

namespace app\common\manage;


use app\common\bean\AdminBean;
use app\common\bean\GlobalBean;
use app\common\bean\StoreBean;
use app\common\bean\StoreUserBean;
use app\common\enum\StoreUserStatusEnum;
use app\common\exception\MyException;
use app\common\model\AdminModel;
use app\common\model\StoreModel;
use app\common\model\StoreUserModel;
use app\common\utils\TimeUtil;
use app\common\validate\AdminValidate;
use app\common\validate\StoreUserValidate;

class LoginManage
{
    protected static $_self = null;
    static function getInstance(){
        if(empty(self::$_self)){
            self::$_self = new LoginManage();
        }
        return self::$_self;
    }

    /** 主后台登录验证
     * @param AdminBean $adminBean
     * @return bool
     * @throws \Exception
     */
    public function adminLogin(AdminBean $adminBean){
        AdminValidate::getInstance()->goCheck(AdminValidate::$login,$adminBean->getData(),[AdminBean::$username => 'require',AdminBean::$password => 'require']);
        $result = AdminModel::getInstance()->selectData(array(AdminBean::$username => $adminBean->getUsername()));
        $result ?: MyException::throwMyException("无管理员信息");
        ($result[AdminBean::$password] == md5($adminBean->getPassword())) ?: MyException::throwMyException("密码错误");
        $adminBean->setData($result);
        session(GlobalBean::$super,true);//主后台用户
        session(GlobalBean::$username,$adminBean->getUsername());//用户名
        session(GlobalBean::$adminUserId,$adminBean->getId());//登录的用户id
        session(GlobalBean::$adminRoleId,$adminBean->getAdminRoleId());//登录的用户角色
        register_shutdown_function(function() use ($adminBean){
            AdminModel::getInstance()->updatedData(array(AdminBean::$id => $adminBean->getId()),array(AdminBean::$ip => request()->ip(),AdminBean::$loginAt => TimeUtil::getInstance()->getTimeNow()));
        });
        return true;
    }

    /** 商家
     * @param StoreUserBean $storeUserBean
     * @return StoreUserBean
     * @throws \think\Exception
     * @throws \Exception
     */
    public function storeLogin(StoreUserBean $storeUserBean){
        StoreUserValidate::getInstance()->goCheck(StoreUserValidate::$login,$storeUserBean->getData(),[StoreUserBean::$username => 'require',StoreUserBean::$password => 'require']);
        $result = StoreUserModel::getInstance()->selectData(array(StoreUserBean::$username => $storeUserBean->getUsername()));
        $result ?:MyException::throwMyException("无商家用户信息");
        //$result[StoreUserBean::$error] <= GlobalBean::$loginErrorCount ?: MyException::throwMyException("密码错误超过次数");
        //$result[StoreUserBean::$status] !== StoreUserStatusEnum::BAN ?: MyException::throwMyException("用户被禁用");
        if($result[StoreUserBean::$password] !== md5($storeUserBean->getPassword())){
            StoreUserModel::getInstance()->where(StoreUserBean::$id,$result[StoreUserBean::$id])->setInc(StoreUserBean::$error);
            MyException::throwMyException("密码错误");
        }
        $storeUserBean->setData($result);
        $storeInfo = StoreModel::getInstance()->selectData([StoreBean::$id => $storeUserBean->getStoreId()]);
        session(GlobalBean::$username,$storeUserBean->getUsername());
        session(GlobalBean::$storeUserId,$storeUserBean->getId());
        session(GlobalBean::$storeRoleId,$storeUserBean->getStoreRoleId());
        session(GlobalBean::$storeId,$storeUserBean->getStoreId());
        session(GlobalBean::$storeName,$storeInfo[StoreBean::$companyName]);
        register_shutdown_function(function () use ($storeUserBean){
            StoreUserModel::getInstance()->updatedData(array(StoreUserBean::$id => $storeUserBean->getId()),array(StoreUserBean::$logintime => TimeUtil::getInstance()->getTimeNow(),StoreUserBean::$error => 0,StoreUserBean::$ip => request()->ip()));
        });
        return $storeUserBean;
    }
}